package handler

import (
	"context"
	"github.com/pkg/errors"
	"server/basic/dao"
	"server/basic/proto/management"
)

type Server struct {
	management.UnimplementedManagementServerServer
}

func (s *Server) DoctorLogin(_ context.Context, req *management.DoctorLoginReq) (*management.DoctorLoginResponse, error) {
	login, err := dao.Login(req.Name)
	if err != nil {
		return nil, errors.New("错误")
	}
	if login.Id == 0 {
		return nil, errors.New("医生不存在")
	}
	if login.Password != req.Password {
		return nil, errors.New("密码错误")
	}
	return &management.DoctorLoginResponse{DoctorId: int32(login.Id)}, nil
}
func (s *Server) PatientList(_ context.Context, req *management.PatientListReq) (*management.PatientListResponse, error) {
	patient, err := dao.Patient(req.DoctorId, req.State)
	if err != nil {
		return nil, err
	}
	var patientList []*management.PatientList
	for _, patients := range patient {
		patientList = append(patientList, &management.PatientList{
			DockerId:       int32(patients.DockerId),
			PatientName:    patients.PatientName,
			PatientImage:   patients.PatientImage,
			PatientSpace:   patients.PatientSpace,
			PatientGender:  patients.PatientGender,
			PatientAge:     patients.PatientAge,
			PatientPolicy:  patients.PatientPolicy,
			PatientRecord:  patients.PatientRecord,
			PatientAllergy: int32(patients.PatientAllergy),
			PatientState:   int32(patients.PatientState),
		})
	}
	return &management.PatientListResponse{Patient: patientList}, nil
}
func (s *Server) Patients(_ context.Context, req *management.PatientsReq) (*management.PatientsResponse, error) {
	patient, err := dao.Patients(req.PatientId)
	if err != nil {
		return nil, err
	}
	var patients []*management.PatientList

	patients = append(patients, &management.PatientList{
		DockerId:       int32(patient.DockerId),
		PatientName:    patient.PatientName,
		PatientImage:   patient.PatientImage,
		PatientSpace:   patient.PatientSpace,
		PatientGender:  patient.PatientGender,
		PatientAge:     patient.PatientAge,
		PatientPolicy:  patient.PatientPolicy,
		PatientRecord:  patient.PatientRecord,
		PatientAllergy: int32(patient.PatientAllergy),
		PatientState:   int32(patient.PatientState),
	})

	return &management.PatientsResponse{Patient: patients}, nil
}
